package vptree;

import java.util.ArrayList;
import java.util.List;

import org.junit.Assert;
import org.junit.Test;

/**
 * @author Anatoly Borisov
 */
public class VpTreeNodeTest {

    @Test
    public void testSimple() {
        List<VpTreePoint2D> points = new ArrayList<VpTreePoint2D>();
        for (int i = 0; i < 1000; ++i) {
            points.add(new VpTreePoint2D(i, i));
        }
        VpTreeNode<VpTreePoint2D> node = VpTreeNode.buildVpTree(points);
        List<VpTreePoint2D> nearbyPoints = node.findNearbyPoints(new VpTreePoint2D(55.1, 55.2), 1.5);

        Assert.assertTrue(nearbyPoints.contains(new VpTreePoint2D(55, 55)));
        Assert.assertTrue(nearbyPoints.contains(new VpTreePoint2D(56, 56)));

        nearbyPoints = node.findNearbyPoints(new VpTreePoint2D(10.1, 10.5), 0.6);
        Assert.assertTrue(nearbyPoints.contains(new VpTreePoint2D(10, 10)));
        Assert.assertEquals(1, nearbyPoints.size());
    }

    @Test
    public void test() {
        List<VpTreePoint2D> points = new ArrayList<VpTreePoint2D>();

        for (int i = 0; i < 5000000; ++i) {
            points.add(new VpTreePoint2D(Math.random() * 10, Math.random()));
        }

        for (int i = 0; i < 5000000; ++i) {
            points.add(new VpTreePoint2D(5 + Math.random() * 5, Math.random()));
        }

        long start = System.currentTimeMillis();
        System.out.println("Building VP-tree...");
        VpTreeNode<VpTreePoint2D> node = VpTreeNode.buildVpTree(points);
        System.out.println("VP-tree completed, took " + (System.currentTimeMillis() - start) / 1000. + " s");
        start = System.nanoTime();
        List<VpTreePoint2D> nearbyPoints = node.findNearbyPoints(new VpTreePoint2D(0.1, 0.1), 0.001);
        System.out.println("VP-tree search completed, took " + (System.nanoTime() - start) + " ns");
        for (VpTreePoint2D p : nearbyPoints) {
            System.out.println(p);
        }
    }
}
